package com.kehongborui.fliggy.service.impl;

import com.kehongborui.fliggy.entity.Station;
import com.kehongborui.fliggy.enums.StationStatus;
import com.kehongborui.fliggy.exception.FliggyException;
import com.kehongborui.fliggy.mapper.StationMapper;
import com.kehongborui.fliggy.model.StationModel;
import com.kehongborui.fliggy.service.StationService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

import static com.kehongborui.fliggy.enums.ExceptionMessage.STATION_ID_IS_NOT_EXIST;
import static com.kehongborui.fliggy.enums.ExceptionMessage.STATION_INFO_IS_EXIST;
import static com.kehongborui.fliggy.enums.ExceptionMessage.STATION_IS_NOT_EXIST;

/**
 * @author zhang peng yang ming
 * @CopyRight (C) http://www.csdn.com
 * @brief
 * @email 122162005@qq.com
 * @date 2018-08-20
 */
@Service
public class StationServiceImpl implements StationService {
    @Autowired
    private StationMapper stationMapper;

    @Override
    public boolean create(StationModel stationModel) {
        // 1.查找数据库中是否已经存在该车站名称对应的信息
        Station selectStation = stationMapper.findByName(stationModel.getName());
        if (selectStation != null) {
            // 车站信息存在，则抛出异常，提示用户
            throw new FliggyException(String.format(STATION_INFO_IS_EXIST.getDesc(), stationModel.getName()));
        }

        Station station = new Station();
        BeanUtils.copyProperties(stationModel, station);
        station.setCreateTime(new Date());
        // 2.向数据库表插入对应的station 信息
        int ret = stationMapper.insertSelective(station);
        return ret > 0;
    }

    @Override
    public boolean update(StationModel stationModel) {
        // 判断id是否存在
        if (stationModel.getId() == null) {
            throw new FliggyException(String.format(STATION_ID_IS_NOT_EXIST.getDesc(), stationModel.getName()));
        }

        // 判断当前数据是否存在于数据库
        Station retStation = stationMapper.selectByPrimaryKey(stationModel.getId());
        if (retStation == null) {
            throw new FliggyException(String.format(STATION_IS_NOT_EXIST.getDesc(), stationModel.getId()));
        }

        Station station = new Station();
        BeanUtils.copyProperties(stationModel, station);
        station.setUpdateTime(new Date());

        int ret = stationMapper.updateByPrimaryKeySelective(station);
        return ret > 0;
    }

    @Override
    public StationModel findByName(String name) {
        Station station = stationMapper.findByName(name);
        StationModel stationModel = new StationModel();
        BeanUtils.copyProperties(station, stationModel);
        return stationModel;
    }

    @Override
    public StationModel findById(Long id) {
        Station station = stationMapper.selectByPrimaryKey(id);
        StationModel stationModel = new StationModel();
        BeanUtils.copyProperties(station, stationModel);
        return stationModel;
    }

    @Override
    public boolean delete(Long id) {
        Station station = new Station();
        station.setId(id);
        station.setUpdateTime(new Date());
        // 禁用该车站
        station.setStatus(StationStatus.DISABLED.getCode());

        int ret = stationMapper.updateByPrimaryKeySelective(station);
        return ret > 0;
    }
}
